package com.unlcn.ils.kas.rest;

import com.alibaba.fastjson.JSON;
import com.unlcn.ils.kas.dao.model.order.bo.ErpOrderSelectHeadBo;
import com.unlcn.ils.kas.service.bo.ErpOrderSelectHead;
import com.unlcn.ils.kas.service.order.*;
import com.unlcn.ils.kas.service.util.enums.CommonExceptionEnum;
import com.unlcn.ils.kas.vo.CommonVO;
import com.unlcn.ils.kas.vo.RestVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单rest
 * @author 罗必量
 */
@RestController
@RequestMapping(value = "/api/order")
@Api(value = "/api/order", description = "订单接口")
public class OrderRest {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderRest.class);

    @Autowired
    private OrderOtdService orderOtdService;

    @Autowired
    private GetGfOrderService getGfOrderService;

    @Autowired
    private GetKgOrderService getKgOrderService;

    @Autowired
    private GetOrderOnWayService getOrderOnWayService;

    @Autowired
    private PushOrderInfoService pushOrderInfoService;

    @RequestMapping(value = "/orderSelect", method = RequestMethod.GET)
    @ApiOperation(value = "查询订单信息", notes = "查询订单信息(状态为正常)", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestVO<ErpOrderSelectHeadBo> orderSelect(Long startTime, Long endTime, String customerNo) {
        LOGGER.info("OrderRest.orderSelect（查询订单信息） params : {} ", startTime, endTime, customerNo);

        RestVO<ErpOrderSelectHeadBo> restVO = null;

        try {
            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), orderOtdService.orderSelect(startTime, endTime, customerNo), CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderController.orderSelect ERROR Message : {} ", e);
            restVO = new RestVO<>(CommonExceptionEnum.ERROR.getCode(),CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }

    @RequestMapping(value = "/getOrderNo", method = RequestMethod.GET)
    @ApiOperation(value = "获取某时间段订单号", notes = "获取某时间段订单号", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestVO<ErpOrderSelectHead> getOrderNo(Long startTime, Long endTime, String customerNo) {
        LOGGER.info("OrderRest.getOrderNo（获取某时间段订单号） params : {} ", startTime, endTime, customerNo);

        RestVO<ErpOrderSelectHead> restVO = null;

        try {
            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), orderOtdService.getOrderNo(startTime, endTime, customerNo), CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderController.getOrderNo ERROR Message : {} ", e);
            restVO = new RestVO<>(CommonExceptionEnum.ERROR.getCode(),CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }

    @RequestMapping(value = "/getOrderDetail", method = RequestMethod.GET)
    @ApiOperation(value = "获取订单详情、轨迹", notes = "获取订单详情、轨迹", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestVO<ErpOrderSelectHead> getOrderDetail(String sysOrderNo) {
        LOGGER.info("OrderRest.getOrderDetail（获取订单详情、轨迹） params : {} ", sysOrderNo);

        RestVO<ErpOrderSelectHead> restVO = null;

        try {
            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), orderOtdService.getOrderDetail(sysOrderNo), CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderController.getOrderDetail ERROR Message : {} ", e);
            restVO = new RestVO<>(CommonExceptionEnum.ERROR.getCode(),CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }

    @RequestMapping(value = "/getOrder", method = RequestMethod.POST)
    @ApiOperation(value = "拉取订单", notes = "拉取订单", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestVO<Map<String, Object>> getGfOrder(@RequestBody CommonVO commonVO) {
        LOGGER.info("OrderRest.getGfOrder（拉取订单） params : {} ", JSON.toJSONString(commonVO));

        RestVO<Map<String, Object>> restVO = null;

        try {
            Map<String, Object> map = new HashMap<>();

            if (commonVO.getCustomerNo() == 1) {
                map = getGfOrderService.getOrder(commonVO.getStartTime(), commonVO.getEndTime());
            } else if (commonVO.getCustomerNo() == 2) {
                map = getKgOrderService.getOrder(commonVO.getStartTime(), commonVO.getEndTime());
            }

            restVO = new RestVO(0, map, "成功拉取");
        } catch (Exception e) {
            LOGGER.error("OrderController.getGfOrder ERROR Message : {} ", e);
            restVO = new RestVO(-1, "系统异常");
        }

        return restVO;
    }

    @RequestMapping(value = "/getOnWay", method = RequestMethod.POST)
    @ApiOperation(value = "根据订单号获取在途", notes = "根据订单号获取在途", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestVO<List<Map<String, String>>> getOnWay(@RequestBody CommonVO commonVO) {
        LOGGER.info("OrderRest.getOnWay（根据订单号获取在途） params : {} ", commonVO.getSysOrderNo());

        RestVO<List<Map<String, String>>> result = null;

        try {
            if (StringUtils.isEmpty(commonVO.getSysOrderNo())) {
                result = new RestVO(-1, null, "订单号参数sysOrderNo不能为空（以逗号分隔）");
                return  result;
            }

            String[] sysOrderNos = commonVO.getSysOrderNo().split(",");

            List<Map<String, String>> list = getOrderOnWayService.getOnWay(sysOrderNos);

            result = new RestVO(0, list, "成功拉取");
        } catch (Exception e) {
            LOGGER.error("OrderController.getOnWay ERROR Message : {} ", e);
            result = new RestVO(-1, "系统异常");
        }

        return result;
    }

    @RequestMapping(value = "/getOnWayBySql", method = RequestMethod.POST)
    @ApiOperation(value = "根据 sql 获取在途", notes = "根据 sql 获取在途", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestVO<String> getOnWayBySql(@RequestBody CommonVO commonVO) {
        LOGGER.info("OrderRest.getOnWay（根据 sql 获取在途） params : {} ", commonVO.getSql());

        RestVO<String> result = null;

        try {
            if (StringUtils.isEmpty(commonVO.getSql())) {
                result = new RestVO(-1, null, "sql 不能为空");
                return  result;
            }

            int count = getOrderOnWayService.getOnWayBySql(commonVO.getSql());

            result = new RestVO(0, "成功拉取" + count + "条订单");
        } catch (Exception e) {
            LOGGER.error("OrderController.getOnWay ERROR Message : {} ", e);
            result = new RestVO(-1, "系统异常");
        }

        return result;
    }

    @RequestMapping(value = "/updateOnWayId", method = RequestMethod.POST)
    @ApiOperation(value = "更新在途轨迹 id", notes = "更新在途轨迹 id", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestVO<String> updateOnWayId(@RequestBody CommonVO commonVO) {
        LOGGER.info("OrderRest.updateOnWayId（更新在途轨迹 id） params : {} ", commonVO.getSql());

        RestVO<String> result = null;

        try {

            int count = getOrderOnWayService.updateOnWayId(commonVO.getStartTime(), commonVO.getEndTime());

            result = new RestVO(0, "成功更新" + count + "条轨迹");
        } catch (Exception e) {
            LOGGER.error("OrderController.getOnWay ERROR Message : {} ", e);
            result = new RestVO(-1, "系统异常");
        }

        return result;
    }

    @RequestMapping(value = "/pushOrderByOrderNo", method = RequestMethod.POST)
    @ApiOperation(value = "根据订单号拉取订单", notes = "根据订单号拉取订单", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestVO<String> pushOrderByOrderNo(@RequestBody CommonVO commonVO) {
        LOGGER.info("OrderRest.pushOrderByOrderNo（根据订单号拉取订单） params : {} ", commonVO);

        RestVO<String> result = null;

        try {

            if (StringUtils.isEmpty(commonVO.getSysOrderNo())) {
                result = new RestVO(-1, null, "订单号参数sysOrderNo不能为空（以逗号分隔）");
                return  result;
            }

            String[] sysOrderNos = commonVO.getSysOrderNo().split(",");

            result = new RestVO(0, pushOrderInfoService.pushOrderByOrderNo(sysOrderNos), "拉取完成");
        } catch (Exception e) {
            LOGGER.error("OrderController.getOnWay ERROR Message : {} ", e);
            result = new RestVO(-1, "系统异常");
        }

        return result;
    }
}
